package edu.xidian.sei.mlcs

import scala.collection.mutable.HashSet

class Sequence(val chars: Array[Char]) {

  def charsets(): Set[Char] = chars.toSet

  def charAt(index: Int) = chars(index)

  def length = chars.length

  def buildSuccessors(c: Char): Array[Short] = {
    val length = chars.length
    val successors = new Array[Short](length)
    var i = 0
    var j = i + 1
    while (i < length) {
      if (j == i) j += 1
      while (j < length && chars(j) != c) {
        j += 1;
      }
      if (j == length) successors(i) = -1;
      else successors(i) = j.toShort;
      i += 1;
    }
    successors
  }

}

object Sequence {
  def build(s: String): Sequence = {
    val chars = new Array[Char](s.length() + 1)
    chars(0) = '.'
    s.getChars(0, s.length(), chars, 1)
    new Sequence(chars)
  }
}